﻿package mortal.game.manager.msgTip
{
    import com.gengine.global.*;
    import com.gengine.utils.*;
    import com.greensock.*;
    import com.greensock.easing.*;
    import flash.display.*;
    import flash.geom.*;
    import flash.text.*;
    import flash.utils.*;
    import mortal.common.*;
    import mortal.common.font.*;
    import mortal.common.global.*;
    import mortal.component.gconst.*;
    import mortal.game.manager.*;

    public class MsgTaskShowImpl extends Object
    {
        private var _str:String;
        private var _daily:int;
        private var _wordTxt:TextField;
        private var _bmp:Bitmap;
        private var _bmpd:BitmapData;
        private var _tweenIn:TweenMax;
        private var _timerKey:uint;
        private var _tweenOut:TweenMax;
        private var _matrix:Matrix;
        private var _ary:Array;
        private var _lastShowTime:int = 0;
        private const LimitShowTime:int = 10;

        public function MsgTaskShowImpl()
        {
            this._ary = [];
            this._bmp = new Bitmap();
            this._matrix = new Matrix(1, 0, 0, 1, 10, 10);
            return;
        }// end function

        public function showTarget(param1:String, param2:int = 5, param3:Boolean = false) : void
        {
            var _loc_4:* = getTimer();
            if (param3 == true || _loc_4 - this._lastShowTime > this.LimitShowTime + 970 && this._ary.length == 0)
            {
                this._lastShowTime = _loc_4;
            }
            else
            {
                this._ary.push(new Msg(param1, param2));
                return;
            }
            this.showMsg(param1, param2);
            setTimeout(this.showNext, this.LimitShowTime + 1000);
            return;
        }// end function

        private function showNext() : void
        {
            var _loc_1:Msg = null;
            if (this._ary.length > 0)
            {
                _loc_1 = this._ary.shift();
                this.showTarget(_loc_1.str, _loc_1.daily, true);
            }
            return;
        }// end function

        private function showMsg(param1:String, param2:int = 5) : void
        {
            this.dispose();
            this._str = param1;
            this._daily = param2;
            if (!this._wordTxt)
            {
                this._wordTxt = new TextField();
                this._wordTxt.autoSize = TextFieldAutoSize.LEFT;
                if (ParamsConst.instance.proxyType == ProxyType.TW)
                {
                    this._wordTxt.defaultTextFormat = new GTextFormat(FontUtil.songtiName, 32, 16777215, null, null, null, null, TextFormatAlign.LEFT);
                }
                else
                {
                    this._wordTxt.defaultTextFormat = new GTextFormat(FontUtil.xingkaiName, 32, 16777215, null, null, null, null, TextFormatAlign.LEFT);
                }
                this._wordTxt.filters = [FilterConst.taskShowTargetFilter];
                this._wordTxt.selectable = false;
                this._wordTxt.mouseEnabled = false;
                this._wordTxt.height = 100;
            }
            this._wordTxt.htmlText = HTMLUtil.addColor(this._str, "#ffffff");
            this._wordTxt.alpha = 1;
            if (this._bmpd)
            {
                this._bmpd.dispose();
                this._bmpd = null;
            }
            this._bmpd = new BitmapData(this._wordTxt.textWidth + 20, 100, true, 0);
            this._bmpd.draw(this._wordTxt, this._matrix);
            this._bmp.bitmapData = this._bmpd;
            if (this._bmp.parent == null)
            {
                LayerManager.topLayer.addChild(this._bmp);
            }
            this._tweenIn = TweenMax.to(this._bmp, 0.5, {alpha:1, ease:Quint.easeIn, onComplete:this.onTweenInEnd});
            this.stageResize();
            return;
        }// end function

        private function onTweenInEnd() : void
        {
            if (this._daily > 0)
            {
                this._timerKey = setTimeout(this.onTimerEnd, this._daily * 1000);
            }
            return;
        }// end function

        private function onTimerEnd() : void
        {
            this.hide();
            return;
        }// end function

        public function stageResize() : void
        {
            if (this._bmp && this._bmp.parent)
            {
                this._bmp.x = (Global.stage.stageWidth - this._wordTxt.textWidth) / 2;
                this._bmp.y = Global.stage.stageHeight / 2 + (Global.stage.stageHeight / 2 - this._wordTxt.textHeight) / 2 + this._wordTxt.textHeight - 40;
            }
            return;
        }// end function

        public function hide() : void
        {
            if (this._bmp && this._bmp.parent)
            {
                if (this._tweenIn && this._tweenIn.active)
                {
                    this._tweenIn.kill();
                }
                clearTimeout(this._timerKey);
                this._tweenOut = TweenMax.to(this._bmp, 1, {alpha:0, ease:Quint.easeOut, onComplete:this.onTweenOutEnd});
            }
            else
            {
                this.dispose();
            }
            return;
        }// end function

        public function get currentShowTxt() : String
        {
            return this._str;
        }// end function

        private function onTweenOutEnd() : void
        {
            this.dispose();
            return;
        }// end function

        public function dispose() : void
        {
            if (this._tweenIn && this._tweenIn.active)
            {
                this._tweenIn.kill();
            }
            if (this._tweenOut && this._tweenOut.active)
            {
                this._tweenOut.kill();
            }
            clearTimeout(this._timerKey);
            if (this._wordTxt)
            {
                this._wordTxt.text = "";
            }
            if (this._bmp.parent)
            {
                this._bmp.parent.removeChild(this._bmp);
            }
            this._str = null;
            this._daily = 0;
            return;
        }// end function

    }
}

class Msg extends Object
{
    public var str:String;
    public var daily:int;

    function Msg(param1:String, param2:int)
    {
        this.str = param1;
        this.daily = param2;
        return;
    }// end function

}

